/**
 * Created by Administrator on 2017/5/28 0028.
 */



$(function () {
    /*进入页面加载商品列表*/


    $.ajax({
        url: "http://127.0.0.1/daling/cartlist.php",
        type: "get",
        dataType: "json",
        success: function (res) {
            //console.log(res);
            var sum = 0;
            for (var i = 0; i < res.length; i++) {

                //cartImg,cartTitle,cartText,cartPrice,cartPid,cartStatus,cartNum
                var tempHtml = '<ul>';
                tempHtml += '<li><input type="checkbox" class="listRadio" ></li>';
                tempHtml += '<li>';
                tempHtml += '<a href="javascript:;" class="list_img">';
                tempHtml += '<div style="background-image: url(' + res[i].cartImg + ')"></div>';
                tempHtml += '</a>';
                tempHtml += '<a href="javascript:;">';
                tempHtml += '<span>' + res[i].cartTitle + '</span>';
                tempHtml += '<span>' + res[i].cartText + '</span>';
                tempHtml += '</a>';
                tempHtml += '</li>';
                tempHtml += '<li><span>¥</span><span class="list_price">' + res[i].cartPrice + '</span></li>';
                tempHtml += '<li><span class="list_sub">-</span><input type="text" value="' + res[i].cartNum + '" class="cartNumber"><span class="list_add">+</span></li>';
                tempHtml += '<li><span class="font_red">¥</span><span class="font_red font_16 list_sum">' + res[i].cartPrice * res[i].cartNum + '</span></li>';
                tempHtml += '<li><a href="javascript:;" class="list_del" data-del="' + res[i].cartPid + '">删除</a></li>';
                tempHtml += '</ul>';
                $(".cart_list").html($(".cart_list").html() + tempHtml);


            }
            //$(".list_all").html(sum);
        }


    });

    /*总价钱*/
    //console.log($(".list_all").html());

    /*个数栏失去焦点时*/
    $(".cart_list").on("blur",".cartNumber",function () {
        var pPri = $(this).parent().prev().children(".list_price").html();
        var pSum = $(this).val()*pPri;
        $(this).parent().next().children(".list_sum").html(pSum);
    });

    /*增加，减少*/
    $(".cart_list").on("click", ".list_sub", function () {
        if ($(this).next("input").val() > 1) {
            $(this).next("input").val($(this).next("input").val() - 1);
            var pPri = $(this).parent().prev().children(".list_price").html();
            var pNum = $(this).next().val();
            var pSum = pNum * pPri;
            $(this).parent().next().children(".list_sum").html(pSum);
        }
    });

    $(".cart_list").on("click", ".list_add", function () {
        $(this).prev("input").val($(this).prev("input").val() - 0 + 1);
        var pPri = $(this).parent().prev().children(".list_price").html();
        var pNum = $(this).prev().val();
        var pSum = pNum * pPri;
        $(this).parent().next().children(".list_sum").html(pSum);
    });


    /*删除*/
    $(".cart_list").on("click", ".list_del", function () {
        //console.log($(this).data("del"));
        var that = this;
        $.ajax({
            url: "http://127.0.0.1/daling/delelist.php",
            type: "post",
            data: {"delId": $(this).data("del")},
            success: function (res) {
                var arr = JSON.parse(res);
                if (arr.status == 1) {
                    alert(arr.msg);
                    $(that).closest("ul").remove();
                }
            }
        })
    });

   /*$(".cart_list").on("click",".list_sum",function () {
       alert(111);
   })*/



});